package com.xiaowan.user.service.impl;

import com.xiaowan.model.common.dtos.ResponseResult;
import com.xiaowan.model.common.enums.AppHttpCodeEnum;
import com.xiaowan.model.user.PersonalInfo;
import com.xiaowan.user.mapper.PersonalInfoDao;
import com.xiaowan.user.service.PersonalInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 实现功能：
 *
 * @author 作者[admin]
 * @version 1.0.00 2023-04-07 12:25
 */
@Service
public class PersonalInfoServiceImpl implements PersonalInfoService {
    //个人信息
    @Resource
    private PersonalInfoDao personalInfoDao;

    @Override
    public ResponseResult addMessage(PersonalInfo personalInfo) {
        if (personalInfo == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //初始化参数
        personalInfo.setCreateTime(new Date());
        try {
            personalInfoDao.insert(personalInfo);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult findUserById(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        PersonalInfo personalInfo = personalInfoDao.selectById(id);
        if (personalInfo == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(personalInfo);
    }

    @Override
    public ResponseResult updatePersonalInfo(PersonalInfo personalInfo) {
        if (personalInfo == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        personalInfo.setUpdateTime(new Date());
        try {
            personalInfoDao.updateById(personalInfo);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}
